<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>圆环菜单</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="circle-menu">
        <div class="circle-menu-container">
            <div class="circle-menu-item">1</div>
            <div class="circle-menu-item">2</div>
            <div class="circle-menu-item">3</div>
            <div class="circle-menu-item">4</div>
            <div class="circle-menu-item">5</div>
        </div>
        <div class="circle-menu-switch">菜单</div>
    </div>
    <script>
        var angle = 90 / 4;
        var r = 150;
        var item = document.querySelectorAll('.circle-menu-item');
        var menu = document.querySelector(".circle-menu-switch");
        var bBtn = 0;

        menu.addEventListener("click", function () {
            
            if (bBtn % 2 === 0) {
                this.style.WebkitTransform = 'rotate(-360deg)'
                open()
            } else {
                this.style.WebkitTransform = 'rotate(0deg)'
                close()
            }

            bBtn = bBtn + 1;
        })

        function open() {
            for (let i = 0; i < item.length; i++) {
                var ele = item[i];
                // 通过三角函数，计算出 left,top的值
                // sin(角度) = > Math.sin(角度 * Math.PI / 180)
                // cos(角度) = > Math.cos(角度 * Math.PI / 180)
                ele.style.top = Math.ceil(-1 * Math.sin(angle * i * Math.PI / 180) * r) - 50 + 'px';
                ele.style.left = Math.ceil(-1 * Math.cos(angle * i * Math.PI / 180) * r) - 50 + 'px';
                
                // 设置延迟过度效果
                ele.style.WebkitTransform = 'rotate(-720deg)'
                ele.style.WebkitTransitionDelay = 0.1 * i + 's';
            }
        }
        function close() {
            for (let i = item.length-1; i > -1; i--) {
                var ele = item[i];
                ele.style.top = '-50px';
                ele.style.left = '-50px';
                // 设置延迟过度效果
                ele.style.WebkitTransform = 'rotate(0deg)'
                ele.style.WebkitTransitionDelay = 0.1 * (5-i) + 's';
            }
        }

    </script>
</body>

</html>